﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
  public static class DateTimeExtensions
  {
    public static DateTime RoundToMinute(this DateTime dt)
    {
      return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);
    }

    public static TimeSpan RoundToHours(this TimeSpan timeSpan)
    {
      return new TimeSpan((long)timeSpan.TotalHours * TimeSpan.TicksPerSecond);
    }

    public static TimeSpan RoundToSeconds(this TimeSpan timeSpan)
    {
      return new TimeSpan((long)timeSpan.TotalSeconds * TimeSpan.TicksPerSecond);
    }

    public static TimeSpan RoundToMinutes(this TimeSpan timeSpan)
    {
      return new TimeSpan((long)timeSpan.TotalMinutes * TimeSpan.TicksPerMinute);
    }

    public static TimeSpan Round(this TimeSpan time, TimeSpan nearest)
    {
      return new TimeSpan((time.Ticks / nearest.Ticks) * nearest.Ticks);
    }
  }
}
